<template>
  <app-dialog v-model="show" width="900px" style="margin-top: 3%" title="绑定账号" @open="handleOpen()">
    <div class="filter_add">
      <div class="task_name">
        <div class="label">筛选器名称:</div>
        <el-input
          v-model="copyconfig.task_name"
          placeholder="筛选器名称"
          style="width: 700px; height: 32px"
        />
      </div>
      <UserConfig v-if="show" :copyconfig="copyconfig" />
      <HandicapConfig v-if="show" :copyconfig="copyconfig" />
    </div>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" class="hovepcvicecolor" @click="handleSubmit()">确定</el-button>
    </template>
  </app-dialog>
</template>

<script setup>
import UserConfig from './components/UserConfig.vue'
import HandicapConfig from './components/HandicapConfig.vue'
import { ElMessage } from 'element-plus'
import { http } from '@/utils/http'
import { onMounted } from 'vue'
const show = defineModel({ type: Boolean })
const emits = defineEmits()

const props = defineProps({
  editfilterdata: Object,
})

const dialogtitle = ref('')
const copyconfig = ref({
  task_name: '',
  source_users_config: [
    {
      bc_type: '',
      agent: '',
      user: [],
      userlist: [],
    },
  ],
  only_play_pk_types: [],
  event_type: '',
  game_type: '',
  task_code: '',
})

const handleClose = () => {
  show.value = false
}

const handleOpen = () => {
  if (props.editfilterdata.task_code != null && props.editfilterdata.task_code != undefined) {
    dialogtitle.value = '编辑过滤器'
    let a = JSON.parse(JSON.stringify(props.editfilterdata.task_form))
    a.task_code = props.editfilterdata.task_code
    a.source_users_config.forEach(item => {
      item.userlist = []
    })
    copyconfig.value = a
  } else {
    dialogtitle.value = '新增过滤器'
    copyconfig.value = {
      task_name: '',
      source_users_config: [
        {
          bc_type: '',
          agent: '',
          user: [],
          userlist: [],
        },
      ],
      only_play_pk_types: [],
      event_type: '',
      game_type: '',
      task_code: '',
    }
  }
}

const handleSubmit = () => {
  if (copyconfig.value.task_name != '') {
    if (
      copyconfig.value.source_users_config[0].bc_type == '' ||
      copyconfig.value.source_users_config[0].agent == '' ||
      copyconfig.value.source_users_config[0].user.length == 0
    ) {
      ElMessage.error('请完善会员配置')
      return
    }

    let body = JSON.parse(JSON.stringify(copyconfig.value))
    body.source_users_config.forEach(item => {
      item.user = item.users
      delete item.userlist
      delete item.users
    })

    if (body.task_code == '') {
      delete body.task_code
    }

    http.post('robot/board_copy_task', body).then(res => {
      show.value = false
      ElMessage({
        message: res.msg,
        type: 'success',
      })

      emits('refreshList')
    })
  } else {
    ElMessage.error('请输入过滤器名称')
  }
}
</script>

<style lang="scss" scoped>
.filter_add {
  width: 100%;
  //   height: 800px;
  //   background: red;
}

.task_name {
  display: flex;
  margin-bottom: 16px;
  align-items: center;
}
.label {
  width: 92px;
  height: 32px;
  line-height: 32px;
  text-align: right;
  margin-right: 12px;
}
</style>
